package com.thwj.project.app.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.thwj.common.annotation.Log;
import com.thwj.common.enums.BusinessType;
import com.thwj.common.exception.ServiceException;
import com.thwj.framework.web.domain.R;
import com.thwj.framework.web.service.TokenService;
import com.thwj.project.app.api.param.ActivityCancelParam;
import com.thwj.project.app.api.param.ActivityHomeParam;
import com.thwj.project.app.api.param.ActivityParam;
import com.thwj.project.app.api.param.ActivitySignUpParam;
import com.thwj.project.app.api.vo.ActivitySignUpVo;
import com.thwj.project.app.api.vo.ActivityVo;
import com.thwj.project.app.service.AppActivityService;
import com.thwj.project.biz.domain.ActivityCategory;
import com.thwj.project.biz.enums.ActivityTypeEnum;
import com.thwj.project.biz.service.IActivityCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Api(tags="app活动接口")
@RestController
@RequestMapping("/app/activity")
public class AppActivityController {


    @Autowired
    private IActivityCategoryService activityCategoryService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private AppActivityService appActivityService;

    @ApiOperation(value = "获取活动分类列表",notes = "获取活动分类列表")
    @GetMapping("/getActivityCategoryList")
    public R<List<ActivityCategory>> getActivityCategoryList() {
        List<ActivityCategory> categoryList = activityCategoryService.list(
                new QueryWrapper<ActivityCategory>()
                        .eq("valid",true)
                        .orderByDesc("order_weight")
        );
        return R.data(categoryList);
    }

    @ApiOperation(value = "获取活动分页列表 分页只要传页码 大小后端控制",notes = "经纬度的获取在多个页面需要使用 首页 活动详情页都需要 经纬度获取后放在请求的header每个请求都携带")
    @GetMapping("/getActivityNormalPage")
    public R<Page<ActivityVo>> getActivityNormalPage(Page page, ActivityHomeParam homeParam) {
        homeParam.setActivityType(ActivityTypeEnum.activity.getValue());
        // 分页size后端控制
//        page.setSize(2);
        Page<ActivityVo> activityPage = appActivityService.getActivityPage(page,homeParam);
        return R.data(activityPage);
    }

    @ApiOperation(value = "获取派对分页列表 分页只要传页码 大小后端控制",notes = "经纬度的获取在多个页面需要使用 首页 活动详情页都需要 经纬度获取后放在请求的header每个请求都携带")
    @GetMapping("/getActivityPartPage")
    public R<Page<ActivityVo>> getActivityPartPage(Page page, ActivityHomeParam homeParam) {
        homeParam.setActivityType(ActivityTypeEnum.party.getValue());
        // 分页size后端控制
//        page.setSize(20);
        Page<ActivityVo> activityPage = appActivityService.getActivityPage(page,homeParam);
        return R.data(activityPage);
    }

    @ApiOperation(value = "获取派对分页列表",notes = "详情页是公用的 但是作者看活动详情页和普通用户应该要分开")
    @GetMapping("/getActivityDetail")
    public R<ActivityVo> getActivityDetail(@RequestParam Long activityId) {
        ActivityVo activityVo = appActivityService.getActivityDetail(activityId,tokenService.getLoginUser());
        return R.data(activityVo);
    }

    @Log(title = "创建活动", businessType = BusinessType.INSERT)
    @ApiOperation(value = "提交活动信息",notes = "活动信息有个coverpic字段需要根据 当前登录用户信息中 userOffice 判断是否显示")
    @PostMapping("/createActivity")
    public R<String> createActivity(@RequestBody ActivityParam activityParam) {
        appActivityService.createUpdateActivity(activityParam,tokenService.getLoginUser());
        return R.data("操作成功");
    }

    @Log(title = "更新活动", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "更新活动信息[暂不启用前端忽略该接口]",notes = "更新活动信息[暂不启用前端忽略该接口]")
    @PostMapping("/updateActivity")
    public R<String> updateActivity(@RequestBody ActivityParam activityParam) {
        appActivityService.createUpdateActivity(activityParam,tokenService.getLoginUser());
        return R.data("操作成功");
    }

    @Log(title = "取消活动", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "取消活动",notes = "发起人 取消活动 结束之前都可以取消")
    @PostMapping("/cancelActivity")
    public R<String> cancelActivity(@RequestBody ActivityCancelParam activityCancelParam) {
        appActivityService.cancelActivity(activityCancelParam,tokenService.getLoginUser());
        return R.data("操作成功");
    }


    @Log(title = "用户报名活动", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "用户报名活动",notes = "用户报名活动")
    @PostMapping("/userSignUpActivity")
    public R<ActivitySignUpVo> userSignUpActivity(@RequestBody ActivitySignUpParam activitySignUpParam) {
        ActivitySignUpVo activitySignUpVo = appActivityService.userSignUpActivity(activitySignUpParam,tokenService.getLoginUser());
        return R.data(activitySignUpVo);
    }

    @Log(title = "用户取消报名活动", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "用户取消报名活动",notes = "用户取消报名活动")
    @PostMapping("/userCancelSignUpActivity")
    public R<String> userCancelSignUpActivity(@RequestBody ActivitySignUpParam activitySignUpParam) {
        appActivityService.userCancelSignUpActivity(activitySignUpParam,tokenService.getLoginUser());
        return R.data("取消成功");
    }


    @ApiOperation(value = "获取我报名过的活动",notes = "只要报名过都展示 暂时不做搜索")
    @GetMapping("/getUserSignupActivityPage")
    public R<Page<ActivityVo>> getUserSignupActivityPage(Page page, String searchKey) {
        // 分页size后端控制
        Page<ActivityVo> activityPage = appActivityService.getUserSignupActivityPage(page,searchKey,tokenService.getLoginUser());
        return R.data(activityPage);
    }

    @ApiOperation(value = "获取我创建的活动列表",notes = "获取我创建的活动列表 暂时不做搜索")
    @GetMapping("/getUserCreateActivityPage")
    public R<Page<ActivityVo>> getUserCreateActivityPage(Page page, String searchKey) {
        // 分页size后端控制
        Page<ActivityVo> activityPage = appActivityService.getUserCreateActivityPage(page,searchKey,tokenService.getLoginUser());
        return R.data(activityPage);
    }

    @ApiOperation(value = "个人主页我参加过的活动分页列表",notes = "个人主页展示不需要登录")
    @GetMapping("/getUserAssociationActivityPage")
    public R<Page<ActivityVo>> getUserAssociationActivityPage(Page page,@RequestParam(required = false) Long userId) {
        if(userId == null && tokenService.getLoginUser() ==null) {
            throw new ServiceException("前端参数错误，用户id必传");
        }
        // 分页size后端控制
        Page<ActivityVo> activityPage = appActivityService.getUserAssociationActivityPage(page,userId == null ? tokenService.getLoginUser().getUserId() : userId);
        return R.data(activityPage);
    }

    @Log(title = "用户确认报名费用结算", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "用户确认报名费用结算",notes = "用户确认报名费用结算")
    @PostMapping("/commitUserActivitySignUpFee")
    public R<String> commitUserActivitySignUpFee(@RequestBody ActivitySignUpParam activitySignUpParam) {
        appActivityService.commitUserActivitySignUpFee(activitySignUpParam,tokenService.getLoginUser());
        return R.data("确认");
    }

    @ApiOperation(value = "获取商家下活动列表",notes = "获取商家下活动列表")
    @GetMapping("/getMerchantActivityPage")
    public R<Page<ActivityVo>> getMerchantActivityPage(Page page, Long merchantId) {
        // 分页size后端控制
        Page<ActivityVo> activityPage = appActivityService.getMerchantActivityPage(page, merchantId);
        return R.data(activityPage);
    }


    @ApiOperation(value = "个人主页获取用户正在参加的活动列表",notes = "个人主页获取用户正在参加的活动列表")
    @GetMapping("/getUserActivityIngList")
    public R<List<ActivityVo>> getUserActivityIngList(@RequestParam(required = false) Long userId) {
        if(userId == null && tokenService.getLoginUser() ==null) {
            throw new ServiceException("前端参数错误，用户id必传");
        }
        List<ActivityVo> activityVoList = appActivityService.getUserActivityIngList(userId == null ? tokenService.getLoginUser().getUserId() : userId);
        return R.data(activityVoList);
    }

}
